$(document).ready(function() {
	initDepartMent();
	
	$('#chooseAppGroup').click(showParentDeptNo);
	
	$("#fordeptID").dblclick(showParentDeptNo); 
	
});


function checkName(async) {
	var loginAccount = $('#loginAccount').val();
	
	if ('' == loginAccount) {
		$('#checkName').text(" 请填写用户名和密码");	
		$('#submit').css('disabled', true);
		return false;
	}
	
	var flag = true;
	
	var param = {
		'employee.loginAccount' : loginAccount
	};

	var options = {
		url: 'iemployee!checkUser.action',
  	  	data: param,
  	  	async: async,
        success: function(msg){
           if ('success' == msg) {
         	   $('#checkName').html('<img src="images/ok.png" style="display: inline" />');	
           } else {
         	   $('#checkName').text("该登录名已存在，请重新填写");
         	   flag = false;
           }
  	    }
	};
	
	$.ajax(options);
	
	return flag;
}

function checkPassword() {
	var loginPwd = $('#loginPwd').val();
	var confirmLoginPwd = $('#confirmLoginPwd').val();
	
	if (loginPwd.length < 6 || confirmLoginPwd.length < 6) {
		$.messager.alert('确定', '<b>登陆密码必须大于6个字符</b>', 'warning');
		return false;
	}
	
	if (loginPwd != confirmLoginPwd) {
		$('#checkP').text('您输入的确认密码和登录密码不一致!');
		
		return false;
	}
	$('#checkP').text('');
	
	return true;		
}

function check() {
	if (!checkName(false)) {
		$.messager.alert('确定', '<b>您的用户名重复请重新填写</b>', 'warning');
		return false;
	}

	if (!checkPassword()) {
		return false;
	}
	
	var deptId = $.trim($('#deptID').val());
	if ('' == deptId) {
		$.messager.alert('确定', '<b>所属课题组是必选项</b>', 'warning');
		
		return false;
	}
	
	var linkPhone = $.trim($('#linkPhone').val());
	if ('' == linkPhone || linkPhone.length < 6) {
		$.messager.alert('确定', '<b>联系方式为空或联系方式不正确，请重新填写</b>', 'warning');
		
		return false;
	}
	
	var email = $.trim($('#email').val());
	if ('' == email) {
		$.messager.alert('确定', '<b>邮箱不能为空</b>', 'warning');
		
		return false;
	} else {
		 var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		 if (!emailReg.test(email)) {
		 	$.messager.alert('确定', '<b>请输入正确的邮箱格式</b>', 'warning');
		 	return false;
		 }
	}
	
	var workEntity = $.trim($('#workEntity').val());
	if ('' == workEntity) {
		$.messager.alert('确定', '<b>所属单位必须填写</b>', 'warning');
		
		return false;
	}
	
	return true;
}


function showParentDeptNo() {
	$('#win').window("open");
	
	$('#win').window({collapsible:false,
	    minimizable:false,
	    maximizable:false,
	    closable:false,
		closed:false,
		top:"95px",
		tools:[{
	        iconCls:'icon-cancel',
	        handler:function(){
	        	   $('#win').window("close");
	        }
   		 }]
	});
}

/**
 * 初始化机构信息
 */
function initDepartMent() {
	$('#department').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'department!showDepartMent.action',
	    idField: 'deptID',
	    columns:[[
	        {field:'deptName',title:'机构名称',width:100},
	        {field:'deptNo',title:'机构编号',width:100},
	        {field:'payCode',title:'付款账号',width:100},
	        {field:'phone',title:'电话',width:80},
	        {field:'address',title:'联系地址',width:100},
	        {field:'deptID',title:'修改',width:100,align:'center',
	        	    formatter : function(value, row, index) {
							return  '<a href="#" style="color:red;text-decoration:none;" onclick="choose('+ index + ',\'' + value +'\')">选中</a> ';
					}
			}
	    ]],
	     pagination:true,
	     pageList:[10,5],
	     rownumbers: true
	});
}

function choose(index, value) {
	$('#department').datagrid('clearSelections');
	$('#department').datagrid('selectRow', index);
	
	var selected = $('#department').datagrid('getSelected');
	$('#fordeptID').val(selected.deptName);
	$('#deptID').val(selected.deptID);
	
	
	 $('#win').window("close");
	
}